<?php

namespace Yi\Permission\Http\Controllers\Auth;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Yi\Permission\Models\Auth\Role;
use Yi\Permission\Models\User;


class RoleController extends Controller
{
    public $request;
    public $model;

    public function __construct(Request $request, Role $model)
    {
        $this->request = $request;
        $this->model = $model;
    }

    function getLable()
    {
        $data = $this->model->select('id', 'id as value', 'name as label')
            ->orderBy('id')
            ->get();
        return success($data);
    }

    /**
     * 删除角色
     */
    function delete($id)
    {
        $haveUser = User::WhereJsonContains('role_ids', (int) $id)->count();
        if ($haveUser) {
            return fail(10013, "有用户使用该角色，请删除该用户");
        }
        $result = $this->model::findOrFail($id)->delete();
        return success($result);
    }
}
